/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <QDebug>
#include <QApplication>
#include <QWidget>
#include <hilog/log.h>

#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QStackedWidget>
#include <QMainWindow>
#include <QLineEdit>
#include "EmbeddedBridgeQtSide.h"
#include "EmbeddedBridge.h"
#include "IMessageHandler.h"
#include "EmbeddedBridgeJsSide.h"
#include "FlutterEmbeddedQWidget.h"
#include <QFrame>
#include "EmbeddedQWidgetFactory.h"

#define WINDOW_WIDTH 2000
#define WINDOW_HEIGHT 1200
using namespace EB;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget window;
    QObject::connect(EmbeddedBridgeJsSide::instance(), &EmbeddedBridgeJsSide::sigSendData,
                     EmbeddedBridgeQtSide::instance(), &EmbeddedBridgeQtSide::slotReceiveData);
    QObject::connect(EmbeddedBridgeQtSide::instance(), &EmbeddedBridgeQtSide::sigSendData,
                     EmbeddedBridgeJsSide::instance(), &EmbeddedBridgeJsSide::slotReceiveData);
    window.setWindowTitle("Hello, World!"); // 设置窗口标题
    window.resize(WINDOW_WIDTH, WINDOW_HEIGHT);              // 设置窗口大小
    window.setWindowTitle("Hello World App");
    QVBoxLayout *rootLayout = new QVBoxLayout();
    QHBoxLayout *editLayout = new QHBoxLayout();
    QHBoxLayout *ButtonLayout = new QHBoxLayout();
    QHBoxLayout *pageLayout = new QHBoxLayout();
    QHBoxLayout *innerPage = new QHBoxLayout();
    rootLayout->addLayout(editLayout);
    rootLayout->addLayout(ButtonLayout);
    rootLayout->addLayout(pageLayout);
    window.setLayout(rootLayout);
    // 创建 QLineEdit 实例
    QLineEdit *lineEdit = new QLineEdit(&window);
    // 设置占位符文本（当没有输入时显示）
    lineEdit->setPlaceholderText("请输入文本...");
    editLayout->addWidget(lineEdit);
    QPushButton *sendButton = new QPushButton("发送", &window);
    editLayout->addWidget(sendButton);
    QPushButton *leftButton = new QPushButton("创建flutter窗口", &window);
    ButtonLayout->addWidget(leftButton);
    QPushButton *rightButton = new QPushButton("删除flutter窗口", &window);
    ButtonLayout->addWidget(rightButton);
    std::shared_ptr<FlutterEmbeddedQWidget> leftPageWidget = nullptr;
    std::shared_ptr<FlutterEmbeddedQWidget> rightPageWidget = nullptr;
    QFrame *frame = new QFrame(&window);
    pageLayout->addWidget(frame);
    pageLayout->setSizeConstraint(QLayout::SetFixedSize);
    frame->setLayout(innerPage);
    QObject::connect(leftButton, &QPushButton::clicked, [&]() {
        if (leftPageWidget == nullptr) {
            leftPageWidget = EmbeddedQWidgetFactory::CreateEmbeddedQWidget(frame);
            innerPage->addWidget(leftPageWidget->GetInnerWidget());
            EmbeddedBridgeQtSide::instance()->registerMessageHandler(leftPageWidget->GetEventHandler());

            leftPageWidget->GetEventHandler()->setCallBack([lineEdit](const QString &message) {
                qDebug() << "onFlutter message text:" << message;
                lineEdit->setText(message);
            });
        }
    });
    
    QObject::connect(rightButton, &QPushButton::clicked, [&]() {
        if (leftPageWidget != nullptr) {
            qInfo() << "QPushButton::clicked entry ";
            innerPage->removeWidget(leftPageWidget->GetInnerWidget());
            EmbeddedQWidgetFactory::DestroyEmbeddedQWidget(leftPageWidget);
        }
    });

    QObject::connect(sendButton, &QPushButton::clicked, [&]() {
        qDebug() << "QLineEdit text:" << lineEdit->text();
        if (leftPageWidget != nullptr) {
            leftPageWidget->sendMessageToEmbedded(lineEdit->text());
        }
    });
    // 显示主窗口。
    window.show();
    qInfo() << "Hello, Qt For OpenHarmony!";

    return a.exec();
}